<!-- @module approval-travel -->
<!-- @author: YunTao.Li -->
<!-- @description: 考勤申诉审批页面 -->
<!-- @since: 2019-11-27 17:33:35 -->
<template>
  <div>
    <fieldset class="fieldset" style="margin:10px;">
      <legend class="legend-font" v-text="appeal_form_text" />
      <el-form ref="appealInputForm" :model="appealInputForm" :label-position="labelPosition" label-width="120px" size="mini">
        <el-row :gutter="10">
          <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <el-form-item label="组织名称" prop="ouId">
              <el-input v-model="appealInputForm.orgCode" name="orgCode" placeholder="组织名称" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <el-form-item label="部门名称" prop="deptId">
              <el-input v-model="appealInputForm.deptId" name="deptId" placeholder="部门名称" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <el-form-item label="申请单编码" prop="explainCode">
              <el-input v-model="appealInputForm.explainCode" name="explainCode" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <el-form-item label="月份" prop="periodCode">
              <el-date-picker v-model="appealInputForm.periodCode" style="width:100%;" type="date" name="periodCode" placeholder="月份" value-format="yyyy-MM" format="yyyy-MM" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
            <el-form-item label="员工姓名" prop="empName">
              <el-input v-model="appealInputForm.empName" name="empName" placeholder="员工姓名" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </fieldset>

    <!-- table begin-->
    <div class="div-table">
      <fieldset class="fieldset" style="margin-top:15px;">
        <legend class="legend-font" v-text="text_grid" />
        <el-table :data="appealInputForm.attendanceAppealDetailList" border fit highlight-current-row style="width: 100%" max-height="350" size="mini">
          <el-table-column align="center" type="index" label="序号" />
          <!--考勤日期-->
          <el-table-column align="center" label="考勤日期" width="250px">
            <template slot-scope="scope">
              <span>{{ scope.row.attendDate | parseTime('{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <!--时段 1-上午，2-下午，3-全天'-->
          <el-table-column align="center" label="时段">
            <template slot-scope="scope">
              <span>{{ scope.row.timeType }}</span>
            </template>
          </el-table-column>
          <!--实际打卡时间-->
          <el-table-column align="center" label="实际打卡时间">
            <template slot-scope="scope">
              <span>{{ scope.row.signDate }}</span>
            </template>
          </el-table-column>
          <!--考勤状态 SIGN_STATUS 0:正常  1:异常  2:漏签-->
          <el-table-column prop="signStatus" :formatter="signStatus" label-class-name="signStatus" align="center" label="考勤状态" />
          <!--申诉原因-->
          <el-table-column label="申诉原因" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.explainDesc }}</span>
            </template>
          </el-table-column>
        </el-table>
      </fieldset>
    </div>
    <!-- table end -->

    <!--file begin-->
    <fileUpload :param-pa-doc-id="appealInputForm.fileParamPaDocId" :disable-upload-button="true" :disable-delete-button="true" />
    <!--file end-->

    <div slot="footer" class="dialog-footer" style="margin:10px;text-align: center">
      <!-- <el-button type="warning" @click="onLoadAppeal">加载待审批的出差单</el-button> -->
      <el-button type="success" @click="onApproved">审批通过</el-button>
      <el-button type="danger" @click="onUnApproved">审批驳回</el-button>
    </div>
  </div>

</template>

<script>
import { getAppealInfoForApproval, approveAppeal, approveAppealDisagree } from '@/api/attendance/approval-service'
import fileUpload from '@/views/attendance/components/fileupload'

export default {
  components: { fileUpload },
  data() {
    return {
      labelPosition: 'right',
      appeal_form_text: '考勤申诉单信息',
      text_grid: '考勤记录',
      appealInputForm: {
        fileParamPaDocId: '',
        explainId: '',
        ouId: '',
        orgCode: '',
        deptId: '',
        explainCode: '',
        insCode: '',
        taskId: '',
        periodCode: '',
        empId: '',
        attendanceAppealDetailList: []
      },
      formLabelWidth: '120px'
    }
  },
  created() {
    // 接收从待办跳转带过来的待办参数
    const processInstID = localStorage.getItem('processInstID')
    this.appealInputForm.insCode = processInstID

    this.appealInputForm.explainCode = ''

    // step1 : 清空随同人员列表
    this.appealInputForm.attendanceAppealDetailList.splice(0, this.appealInputForm.attendanceAppealDetailList.length)
  },
  mounted() {
    this.onLoadAppeal()
  },
  methods: {
    onLoadAppeal() { // 获得数据
      const insCode = this.appealInputForm.insCode
      var _this = this
      getAppealInfoForApproval(insCode).then(response => {
        this.appealInputForm = response.data
        const taskId = localStorage.getItem('taskId')
        this.appealInputForm.taskId = taskId

        //* ***file begin */
        _this.appealInputForm.fileParamPaDocId = _this.appealInputForm.explainId
        /** **file end */
      })
    },
    onUnApproved() { // 审批驳回
      const explainId = this.appealInputForm.explainId
      const data = this.appealInputForm

      this.$confirm('确定要驳回审批么?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        approveAppealDisagree(explainId, data).then(response => {
          if (response.code === 200) {
            this.$message({ type: 'success', message: '审批驳回成功!' })

            setTimeout(() => {
              this.$router.go(-1)
            }, 500)
          } else {
            this.$message.error(response.message)
          }
        })
      }).catch(err => { console.error(err) })
    },
    onApproved() { // 审批通过
      const explainId = this.appealInputForm.explainId
      const data = this.appealInputForm

      this.$confirm('确定要通过审批么?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        approveAppeal(explainId, data).then(response => {
          if (response.code === 200) {
            this.$message({ type: 'success', message: '审批成功!' })
            setTimeout(() => {
              this.$router.go(-1)
            }, 500)
          } else {
            this.$message.error(response.message)
          }
        })
      }).catch(err => { console.error(err) })
    }
  }
}
</script>

<style>

</style>
